package com.min.common.entity;

import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class BaseController<T extends BaseEntity, S extends IService<T>> {

    @Autowired
    protected S service;

    @GetMapping("/id/{id}")
    public R one(@PathVariable("id") Long id){
        return R.ok().put("data", service.getById(id));
    }

    @GetMapping
    public R list(T entity){
        List list = service.lambdaQuery().setEntity(entity).list();
        return R.ok().put("list", list);
    }

    @RequestMapping("/page")
    public R list(@RequestBody QueryPage<T> page){
        final QueryPage<T> pageResults = service.lambdaQuery().setEntity(page.getQuery()).page(page);
        return R.ok().put("page", pageResults);
    }

    @PostMapping
    public R add(@RequestBody T t){
        return service.save(t) ? R.ok() : R.error();
    }

    @PutMapping
    public R edit(@RequestBody T t){
        return service.updateById(t) ? R.ok() : R.error();
    }

    @DeleteMapping("/{id}")
    public R remove(@PathVariable Long id){
        return service.removeById(id) ? R.ok() : R.error();
    }

    @DeleteMapping
    public R remove(@RequestBody List<Long> ids){
        return service.removeByIds(ids) ? R.ok() : R.error();
    }

}
